package elion.com.theElion.util.adapter;

import android.content.Context;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.zzhoujay.richtext.RichText;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

import elion.com.elion.R;
import elion.com.theElion.util.ReflectUtil;

import static elion.com.theElion.util.ClassAttrsName.getAttrsName;

/**
 * Created by xiaon on 2017/6/8.
 */

public class ServiceAdapter extends BaseAdapter {

    private List<Map<String, String>> list;
    private LayoutInflater layoutInflater;
    private String name[];
    private Object object;

    public ServiceAdapter(Context context, List<Map<String, String>> list, Object object) {
        this.layoutInflater = layoutInflater.from(context);
        this.list = list;
        this.object = object;
        this.name = getAttrsName(object);
    }

    public ServiceAdapter(List<Map<String, String>> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View mView, ViewGroup parent) {

        ViewHole viewHole;
        if (mView == null) {
            mView = layoutInflater.inflate(R.layout.listview_my_item, null);
            viewHole = new ViewHole();
            viewHole.title = (TextView) mView.findViewById(R.id.sevice_fuwu_title);
            viewHole.content = (TextView) mView.findViewById(R.id.sevice_fuwu_text);
//            content为需要解析出图片的TextView
            viewHole.answer = (TextView) mView.findViewById(R.id.sevice_fuwu_answer);
            viewHole.date = (TextView) mView.findViewById(R.id.sevice_fuwu_date);
            mView.setTag(viewHole);
        } else {
            viewHole = (ViewHole) mView.getTag();
        }
        viewHole.content.setMaxLines(3);
        viewHole.answer.setMaxLines(3);

        for (int i = 0; i < name.length; i++) {
            try {
                if (name[i].equals("direction")) {
                    viewHole.content.setTextSize(20);
                }
                ReflectUtil.useSetMethod(viewHole, name[i],
                        Html.fromHtml(list.get(position).get(name[i])).toString());


            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

//        viewHole.content.setText(list.get(position).get("content"));
//        viewHole.date.setText(list.get(position).get("date"));
//        viewHole.answer.setText(list.get(position).get("title"));
//        viewHole.title.setText(list.get(position).get("title"));
        return mView;
    }


    class ViewHole {

        private TextView content;
        private TextView date;
        private TextView answer;
        private TextView title;

        public TextView getContent() {
            return content;
        }

        public void setContent(String content)
        {
//            RichText.from(content).into(this.content);
            this.content.setText(content);
        }

        public TextView getDate() {
            return date;
        }

        public void setDate(String date) {
            this.date.setText(date);
        }

        public TextView getAnswer() {
            return answer;
        }

        public void setAnswer(String answer) {
            this.answer.setText(answer);
        }

        public TextView getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title.setText(title);
        }
    }
}
